package com.yootoo.extpmml.smg3.loader;

import com.experian.stratman.decisionagent.business.IStrategyLoader;
import com.experian.stratman.decisionagent.runtime.DecisionAgentConfig;

import java.io.*;

/**
 * 本地策略加载器
 * 与 com.experian.stratman.decisionagent.business.AlternateStrategyLoader 效果一样
 * 
 * @author GuoZH
 * @version 1.0, 2016-12-27
 */
public class LocalStrategyLoader implements IStrategyLoader {
	
	private static final String STRATEGY_STRAT_PATH = "strategy.path";

	@Override
	public InputStream getStream(String strategyName) throws IOException {
		if(strategyName.endsWith("ser")){
			if(strategyName.startsWith("/")){
				strategyName = strategyName.substring(1);
			}
			String strategyPath = DecisionAgentConfig.instance().getProperty(STRATEGY_STRAT_PATH);
			if(!strategyPath.endsWith("/")){
				strategyPath = strategyPath + "/";
			}
			File file = new File(strategyPath, strategyName);
			if(file.exists()){
				return new FileInputStream(file);
			}else {
				throw new FileNotFoundException("File not found in all configured directories");
			}
		}else {
			throw new FileNotFoundException("File not found in all configured directories");
		}
	}
}
